<?php 
class NewsAdmin extends Module{
	function NewsAdmin($row){
		Module::Module($row);
		
		if (!User::have_permit(ADMIN_NEWS))
		Url::access_denied();
		
		CGlobal::$website_title = 'Quản lý tin tức';
		
		switch(Url::get('cmd'))
		{
			case 'del_img':
			{
				$news_id = Url::get('news_id','int',0);
				
				if($news_id)
				{
					$news = DB::select("news","id=".$news_id);
					
					if($news)
					{
						DB::update('news',array('image'=>'','img_server'=>0,'image_title'=>''),"id=".$news['id'],__LINE__.__FILE__);
					}
				}
				
				if(Url::get('href'))
					Url::redirect_url(urldecode(Url::get('href')));
				else 
					Url::redirect_current();;
			}
			
			case 'pub':
				$this->add_form(new PublicNewsForm());
				break;
			
			case 'delete':		
				$this->add_form(new DeleteNewsForm());		
				break;
			case 'edit':
				CGlobal::$website_title = 'Sửa tin';	
					
				require_once 'forms/NewsAdminEdit.php';
				$this->add_form(new NewsAdminEditForm());		
				break;
			case 'add':		
				CGlobal::$website_title = 'Thêm tin';		
				require_once 'forms/NewsAdminAdd.php';
				$this->add_form(new NewsAdminAddForm());			
				break;		
			default:			
				require_once 'forms/NewsAdmin.php';
				$this->add_form(new NewsAdminForm()); 
		}
	}
}


class PublicNewsForm extends Form
{
	var $news=0;
	
	function __construct()
	{
		Form::Form('PublicNewsForm');
		
		$id = Url::get('id',0);
		
		if($id)
		{
			$this->news = DB::select("news","id=$id");
		}
		
		if (!$this->name)
		{
			Url::redirect('admin_news');
		}
	}

	function on_submit()
	{
		global $oDB;
		$admin_comment	=	Url::get('admin_comment');
		
		$act			=	Url::get('act');
		
		$status = 1;
		
		if ($act=='UnPublish')
		$status = 0;
		
		DB::update('news', array(
								'status'		=>$status,
								'time_modify'	=>TIME_NOW,
								'user_modify'	=>User::user_name(),
								'admin_comment'	=>addslashes(Url::get('admin_comment'))
								),'id='.$this->news['id']);

		if(Url::get('href'))
			Url::redirect_url(urldecode(Url::get('href')));
		else
			Url::redirect_current();
	}
	
	function draw()
	{
		global $display;
		$act=Url::get('act');
		
		$this->beginForm();
		
		if(Url::get('href'))
			$display->add('back',Url::get('href'));
		else
			$display->add('back',Url::build('admin_news'));
			
		
		if ($act=='Publish')	
			$display->add('status_title',"HIỂN THỊ");
		else
			$display->add('status_title',"ẨN");
		
		$display->add('id',$this->news['id']);
		$display->add('title',$this->news['title']);
		$display->add('admin_comment',$this->news['admin_comment']);
		$display->add('status', ($this->news['status'] ? 'Hiển thị' : 'Ẩn'));
		$display->add('cat', "[".$this->news['news_catid']."] ".CGlobal::$allNewsCategories[$this->news['news_catid']]['name']);
		
		$display->add('url', Url::build('news_detail',array('news_catid'=>$this->news['news_catid'],'news_id'=>$this->news['id'])));
				
		$display->output('NewsPublic');
		
		$this->endForm();
	}
}

class DeleteNewsForm extends Form
{
	function __construct()
	{				
		Form::Form('DeleteNewsForm');
	
		$id = Url::get('id',0);
		
		if($id)
		{
			$row = DB::select('news',"id=$id");
			
			if($row)
			{
				DB::delete('news_categories',"id=".$row['id']);
				DB::query("UPDATE news_categories SET position=position-1 WHERE id!=".$row['id']." AND position>=".$row['position']);
				
				AZLib::getNewsCats(1);
			}
		}

		Url::redirect_current();
	}
}
?>